<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("conn.php");

$image_file = $_FILES ['imageURL'] ['name'];
if ($image_file != "") {
    $path_parts = pathinfo($image_file);
    $imageURL = "suggest/" . date("YmdHis") . "." . $path_parts ['extension'];
    move_uploaded_file($_FILES ['imageURL'] ['tmp_name'], $imageURL);
}

$id = checkTrim($_POST['id']);
$position = checkTrim($_POST['position']);
$isModify = $_POST['isModify'];

//$name = $_POST['name'];
$code = $_POST['code'];
$introduce = $_POST['introduce'];

$db = new DBC();

$sql = "SELECT * FROM t_product WHERE flashcode = '" . $code . "' AND status = 1";
$products = $db->fetchData($sql);
if ($products != false && count($products) > 0) {
    $newName = $products[0]['name'];

    $product_code_not_found = false;
    $is_code_exist = false;
    if ($isModify == 1) {
        $clear = false;
        if (isset($_POST['clear']) && $_POST['clear'] == "on") {
            $clear = true;
        }
        if ($_FILES ['imageURL'] ['name'] != "") {
            $sql = "SELECT * FROM t_suggest WHERE id = " . $id;
            $data = $db->fetchData($sql);
            if ($data != false && count($data) > 0) {
                $oldImageURL = $data[0]['imageURL'];
                if ($oldImageURL != "") {
                    unlink($oldImageURL);
                }
            }
            if ($clear) {
                $sql = "UPDATE t_suggest SET name = '" . $newName . "',code = '" . $code . "',imageURL='" . $imageURL . "',introduce = '" . $introduce . "',createTime = now(),click = 0 WHERE id = " . $id;
            } else {
                $sql = "UPDATE t_suggest SET name = '" . $newName . "',code = '" . $code . "',imageURL='" . $imageURL . "',introduce = '" . $introduce . "',createTime = now() WHERE id = " . $id;
            }
        } else {
            if ($clear) {
                $sql = "UPDATE t_suggest SET name = '" . $newName . "',code = '" . $code . "',introduce = '" . $introduce . "',createTime = now(),click = 0 WHERE id = " . $id;
            } else {
                $sql = "UPDATE t_suggest SET name = '" . $newName . "',code = '" . $code . "',introduce = '" . $introduce . "',createTime = now() WHERE id = " . $id;
            }
        }
    } else {
        $sql = "SELECT flashcode FROM t_product WHERE flashcode= '" . $id . "'";
        $data = $db->fetchData($sql);
        if ($data != null && count($data) > 0) {
            $is_code_exist = true;
        }
        $sql = "INSERT INTO t_product(flashcode,name,age,url,imageURL,topImageURL,version,size,type1,type2,packid,createdate) VALUES('" . $flashcode . "','" . $name . "'," . $age . ",'" . $url . "','" . $imageURL . "','" . $topImageURL . "','" . $version . "','" . $size . "','" . $type1 . "','" . $v . "'," . $packid . ",now())";
    }
} else {
    $product_code_not_found = true;
}
if ($product_code_not_found) {
    $msg = "编号为 " . $code . " 的应用不存在或者已下线。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else if ($is_code_exist) {
    $msg = "编号 " . $flashcode . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
//    echo $sql;
    $transfer = "suggestList.php?menu=suggest";
    if ($db->execute($sql)) {
        //success
        //    echo "<script language='javascript'>alert('成功'); </ script>";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
